#!/bin/bash
#功能：
#	基于debian的vagex挂机一键包
#时间：
#	2012/10/7
#作者：
#	爱玩C	www.iwanc.com
#备注：
#	一键安装 iceweasel 、flash 插件、VNC,尽量在screen下运行以防中途断线

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH

vncreboot_addr="http://iwanc.googlecode.com/svn/trunk/vd/vncreboot"
vncserverd_addr="http://iwanc.googlecode.com/svn/trunk/vd/vncserverd"
xstartup_addr="http://iwanc.googlecode.com/svn/trunk/vd/xstartup"
flash_addr="http://iwanc.googlecode.com/svn/trunk/vd/install_flash_player_10_linux.tar.gz"
vagex_addr="http://iwanc.googlecode.com/svn/trunk/vd/vagex_add_on-1.4.4.xpi"
yvqm_addr="http://iwanc.googlecode.com/svn/trunk/vd/youtube_video_quality_manager-1.2-fx.xpi"

func_check() {
	if [ $? != 0 ]; then
		echo "-----------"
		echo "ERROR! -> $1"
		echo "-----------"
		exit 1
	else
		echo "-----------"
		echo "DONE! -> $1"
		echo "-----------"
	fi	
}

func_vncpwd() {
	echo "-----------"
	echo "Plese input the VNC password below!"
	echo "-----------"
}

func_alldone() {
	echo "-----------"
	echo "Now you can VNC to complete the rest of the installation manually!"
	echo "-----------"
}

func_in_flash() {
	wget $flash_addr &&
	tar zxvf install_flash_player_10_linux.tar.gz
	mkdir -p ~/.mozilla/plugins/
	cp libflashplayer.so ~/.mozilla/plugins/
	func_check flash-installation	
}

func_in_ff() {
	apt-get -y --force-yes install iceweasel
	func_check iceweasel-installation	
}

func_in_vnc() {
	aptitude -y install vnc4server &&
	func_vncpwd
	vncserver && vncserver -kill :1 &&
	wget $xstartup_addr &&
	mv ~/.vnc/xstartup ~/.vnc/xstartupbak &&
	mv xstartup ~/.vnc/ && chmod a+x ~/.vnc/xstartup &&
	wget $vncserverd_addr &&
	mv vncserverd /etc/init.d && chmod a+x /etc/init.d/vncserverd &&
	if [ -f /etc/rc.local ]; then
		sed -i '/exit\ 0/d' /etc/rc.local
		cat <<- EOF >> /etc/rc.local
			/etc/init.d/vncserverd start
			exit 0
		EOF
	else
		update-rc.d vncserverd defaults
	fi
	wget $vncreboot_addr &&
	mv vncreboot /etc/cron.daily && chmod a+x /etc/cron.daily/vncreboot
	func_check vnc-installation	
}

wget $vagex_addr
wget $yvqm_addr

apt-get update &&

apt-get -y --force-yes install aptitude wget &&

mkdir -p /tmp/vagex_debian_cache && cd /tmp/vagex_debian_cache &&
func_in_vnc

func_in_ff

func_in_flash

cd /tmp &&

rm -rf /tmp/vagex_debian_cache &&

/etc/init.d/vncserverd start &&

func_alldone
